<%@ page language="java" contentType="text/html; charset=utf-8"
		 pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<link href="../css/eys_stly.css" rel="stylesheet" type="text/css" />
	<link href="../css/bootstrap.css" rel="stylesheet" type="text/css" />
	<link href="../ss/css/bootstrap-table.css" rel="stylesheet" type="text/css" />

</head>
<body>
<script src="../js/jquery.js"></script>
<script src="../js/mustache.js"></script>
<script src="../js/bootstrap.min.js"></script>
<div class=" index_buy">
	<h5 class=" h5_all_s">所在位置：<a>添加采购单</a></h5>

	<title>采购单管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">

        function addRow(list, idx, tpl, row){
            $(list).append(Mustache.render(tpl, {
                idx: idx, delBtn: true, row: row
            }));
            $(list+idx).find("select").each(function(){
                $(this).val($(this).attr("data-value"));
            });

        }
        function delRow(obj, prefix){
            var id = $(prefix+"_id");
            var delFlag = $(prefix+"_delFlag");
            if (id.val() == ""){
                $(obj).parent().parent().remove();
            }else if(delFlag.val() == "0"){
                delFlag.val("1");
                $(obj).html("&divide;").attr("title", "撤销删除");
                $(obj).parent().parent().addClass("error");
            }else if(delFlag.val() == "1"){
                delFlag.val("0");
                $(obj).html("&times;").attr("title", "删除");
                $(obj).parent().parent().removeClass("error");
            }
        }
        function changeSettlementPrice(i) {
            var price=$("#purchaseDetailList"+i+"_price").val();
            if(price.length==0||price==0){
                alert("单价必填");
                return false;
            }
            var amount=$("#purchaseDetailList"+i+"_amount").val();
            if(amount.length==0||amount==0){
                alert("数量必填");
                return false;
            }
            zk();

        }

        function zk(){
            for (var i=0;i<purchaseDetailRowIdx;i++){
                var _marketPrice=$("#purchaseDetailList"+i+"_amount").val();
                var _price=$("#purchaseDetailList"+i+"_price").val();
                $("#purchaseDetailList"+i+"_totalprice").val(_price*_marketPrice);

            }
        }
	</script>

	<body>

	<form id="inputForm"  action="${ctx}/purchase_purchaseOrder/save" method="post" class="form-horizontal">
		<input name="id" type="hidden" value="${purchaseOrder.id}">
		<div class="control-group">
			<label class="control-label">编码：</label>
			<div class="controls">
				<input name="code" htmlEscape="false" value="${purchaseOrder.code} " class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">类型</label>
			<div class="controls">
				<select name="type" class="type">
					<option value="1">采购</option>
					<option value="2">退货</option>
				</select>
				<script type="javascript">
                    $(".type").val('${purchaseOrder.type}');
				</script>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">往来单位</label>
			<div class="controls">
				<select name="company" class="company">
					<c:forEach items="${companys}" var="mc" >
						<option value="${mc.id}"> ${mc.name}</option>
					</c:forEach>
				</select>
				<script type="javascript">
                    $(".company").val('${purchaseOrder.company}');
				</script>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">单据日期：</label>
			<div class="controls">
				<input name="cdate" type="date" value="${purchaseOrder.cdate}" />
			</div>
		</div>
		<%--<div class="control-group">
			<label class="control-label">客户名称：</label>
			<div class="controls">
				<input name="customername" htmlEscape="false"value="${purchaseOrder.customername}"  class="input-xlarge "/>
			</div>
		</div>--%>
		<div class="control-group">
			<label class="control-label">采购部门：</label>
			<div class="controls">
				<input name="dept" htmlEscape="false" value="${purchaseOrder.dept}"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">采购员：</label>
			<div class="controls">
				<input name="user" htmlEscape="false" value="${purchaseOrder.user}"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">采购单明细：</label>
			<div class="controls">
				<table id="contentTable" class="table table-striped table-bordered table-condensed">
					<thead>
					<tr>
						<th class="hide"></th>
						<th>商品编码</th>
						<th>商品名称</th>
						<th>数量</th>
						<th>单价</th>
						<th>总价格</th>
						<th width="10">&nbsp;</th>
					</tr>
					</thead>
					<tbody id="purchaseDetailList">
					</tbody>
					<tfoot>
					<tr><td colspan="7"><a href="javascript:" onclick="addRow('#purchaseDetailList', purchaseDetailRowIdx, purchaseDetailTpl);purchaseDetailRowIdx = purchaseDetailRowIdx + 1;" class="btn">新增</a></td></tr>
					</tfoot>
				</table>
				<script type="text/template" id="purchaseDetailTpl">//<!--
						<tr id="purchaseDetailList{{idx}}">
							<td class="hide">
								<input id="purchaseDetailList{{idx}}_id" name="purchaseDetailList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="purchaseDetailList{{idx}}_delFlag" name="purchaseDetailList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input id="purchaseDetailList{{idx}}_code" name="purchaseDetailList[{{idx}}].code" type="text" value="{{row.code}}" maxlength="20" class="input-small "/>
							</td>
							<td>
								<input id="purchaseDetailList{{idx}}_name" name="purchaseDetailList[{{idx}}].name" type="text" value="{{row.name}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="purchaseDetailList{{idx}}_amount" name="purchaseDetailList[{{idx}}].amount" type="text" value="{{row.amount}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="purchaseDetailList{{idx}}_price" name="purchaseDetailList[{{idx}}].price" onchange="changeSettlementPrice({{idx}})" type="text" value="{{row.price}}" class="input-small "/>
							</td>
							<td>
								<input id="purchaseDetailList{{idx}}_totalprice" name="purchaseDetailList[{{idx}}].totalprice" readonly type="text" value="{{row.totalprice}}" class="input-small "/>
							</td>

						 <td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#purchaseDetailList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td>
						</tr>//-->
				</script>
				<script type="text/javascript">
                    var purchaseDetailRowIdx = 0, purchaseDetailTpl = $("#purchaseDetailTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                    $(document).ready(function() {
                        var data = ${fns:toJson(purchaseOrder.purchaseDetailList)};
                        for (var i=0; i<data.length; i++){
                            addRow('#purchaseDetailList', purchaseDetailRowIdx, purchaseDetailTpl, data[i]);
                            purchaseDetailRowIdx = purchaseDetailRowIdx + 1;
                        }
                    });
				</script>
			</div>
		</div>
		<div class="form-actions">
			<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form>

	</body>
</html>